import Ember from 'ember';
import _ from 'lodash';
import DataTableCellGroupComponent from 'playful/components/data-table-cell-group';

export default DataTableCellGroupComponent.extend({
  actions: {
    toggleSelect: function(){
      this.sendAction("toggleSelect", this.get('localGroup'));
    }
  },
  folder: function(){
    var allDirectories = _.uniq(_.invoke(this.get('group'), 'get', 'audioFile.directory'));
    return allDirectories.length === 1 ? _.first(allDirectories) : false;
  }.property('group', 'group.@each.audioFile.directory'),
  artist: function(){
    var allArtists = _.uniq(_.map(this.get('group'), function(row){
      return row.get('audioFile.albumArtist') || row.get('audioFile.artist');
    }));
  }.property('group', 'group.@each.audioFile.albumArtist', 'group.@each.audioFile.artist'),
  album: function(){
    var allArtists = _.uniq(_.map(this.get('group'), function(row){
      return row.get('audioFile.albumArtist') || row.get('audioFile.artist');
    }));
    var allAlbums = _.uniq(_.invoke(this.get('group'), 'get', 'audioFile.album'));
    return allArtists.length === 1 && allAlbums.length === 1 ? _.first(allAlbums) : false;
  }.property('group', 'group.@each.audioFile.albumArtist', 'group.@each.audioFile.artist'),
  layout: Ember.Handlebars.compile(
    '{{#if album}}Album: {{album}}<br />{{/if}}' +
    '{{#if artist}}Artist: {{artist}}<br />{{/if}}' +
    '{{#if folder}}Folder: {{folder}}<br />{{/if}}' +
    '<button type="button" {{action "toggleSelect"}} class="btn btn-link"> ' +
      '{{#if isLocalGroupSelected}}Deselect{{else}}Select{{/if}} group' +
    '</button> ' +
    '<br />' +
    '{{value}}')
});